package de.wolvan.autoupdate;

import java.io.IOException;

import de.wolvan.references.*;
import de.wolvan.tools.*;

public class UpdateChecker {
	
	public static boolean isUpdateAvailable() {
		double version = getVersionFromServer();
		if(version != 0.0) {
			if(version > metadata.version) {
				System.out.println("New version available");
				return true;
			} else {
				System.out.println("No new Version available");
				return false;
			}
		} else {
			System.out.println("Can't compare Versions, Version fetch routine did not return a valid value");
			return false;
		}
	}
	
	public static String getUpdatePackageURL() {
		try {
			String updateData = LinkTools.getWebsiteSource(ProgramVars.updateURL);
			System.out.println("Update Package URL: " + updateData.split(";")[1]);
			return updateData.split(";")[1];
		} catch (IOException e) {
			System.out.println("Error while fetching Update Package URL");
			e.printStackTrace();
			return "";
		}
	}
	
	private static double getVersionFromServer() {
		try {
			String updateData = LinkTools.getWebsiteSource(ProgramVars.updateURL);
			System.out.println("Newest Version: " + updateData.split(";")[0]);
			return Double.parseDouble(updateData.split(";")[0]);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Error while fetching Newest Version Data");
			return 0.0;
		}
		
	}
	
}
